﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Common;
using Telerik.WinControls.UI.Localization;

namespace QuanLyThuPhiTaiLieu
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            RadGridLocalizationProvider.CurrentProvider = new VnRadGridLocalizationProvider();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //// Add the event handler for handling UI thread exceptions to the event.
            //Application.ThreadException += UiThreadException;

            //// Set the unhandled exception mode to force all Windows Forms errors to go through 
            //// our handler.
            //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            //// Add the event handler for handling non-UI thread exceptions to the event. 
            //AppDomain.CurrentDomain.UnhandledException +=
            //    CurrentDomain_UnhandledException;

            Application.Run(new MDI());
        }

        #region Handler exception

        // Handle the UI exceptions by showing a dialog box, and asking the user whether 
        // or not they wish to abort execution. 
        private static void UiThreadException(object sender, ThreadExceptionEventArgs t)
        {
            DialogResult result = DialogResult.Cancel;
            try
            {
                result = ShowThreadExceptionDialog(Constants.Messages.ErrorMessageCaption, t.Exception);
            }
            catch
            {
                try
                {
                    MessageBox.Show(Constants.Messages.ErrorMessage, Constants.Messages.ErrorMessageCaption,
                                    MessageBoxButtons.AbortRetryIgnore,
                                    MessageBoxIcon.Stop);
                }
                finally
                {
                    Application.Exit();
                }
            }

            // Exits the program when the user clicks Abort. 
            if (result == DialogResult.Abort)
                Application.Exit();
        }

        // Handle the UI exceptions by showing a dialog box, and asking the user whether 
        // or not they wish to abort execution. 
        // NOTE: This exception cannot be kept from terminating the application - it can only  
        // log the event, and inform the user about it.  
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = (Exception)e.ExceptionObject;

                // Since we can't prevent the app from terminating, log this to the event log. 
                if (!EventLog.SourceExists("ThreadException"))
                {
                    EventLog.CreateEventSource("ThreadException", "CIAG_Application");
                }

                // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source = "ThreadException";
                myLog.WriteEntry(ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);
            }
            catch (Exception exc)
            {
                try
                {
                    //MessageBox.Show(Constants.Messages.ErrorMessage, Constants.Messages.ErrorMessageCaption, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
                    MessageBox.Show(exc.StackTrace, Constants.Messages.ErrorMessageCaption, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
                }
                finally
                {
                    Application.Exit();
                }
            }
        }

        // Creates the error message and displays it. 
        private static DialogResult ShowThreadExceptionDialog(string title, Exception e)
        {
            return MessageBox.Show(Constants.Messages.ErrorMessage, title, MessageBoxButtons.AbortRetryIgnore,
                MessageBoxIcon.Stop);
        }
        #endregion
    }
}
